﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Logistics_Management_System.Customer_Employee_Permission
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Customer_Service" in code, svc and config file together.
    public class Customer_Service : ServiceBase, ICustomer_Service
    {
        private Customer_Repository cust_repo = new Customer_Repository();
        public List<LG_Customer> GetAllCustomers()
        {
            return (from cust in this.DB.LG_Customers select cust).ToList<LG_Customer>();
        }

        public Objects.ReturnBool AddNewCutomer(LG_Customer new_cust)
        {
            if (cust_repo.GetCustomerByIdentityCard(new_cust.Identity_Card) == null)
            {
                this.DB.LG_Customers.InsertOnSubmit(new_cust);
                this.DB.SubmitChanges();
                return new Objects.ReturnBool() { Value = true };
            }
            else { return new Objects.ReturnBool() { Value = false, Reason = "Customer is exist by Duplicate Identity Card Number " + new_cust.Identity_Card }; }
        }

        public Objects.ReturnBool EditInfoCustomer(int cust_id, LG_Customer updated)
        {
            var existing = cust_repo.GetCustomerById(cust_id);
            if (existing != null)
            {
                var existing_identity = cust_repo.GetCustomerByIdentityCard(updated.Identity_Card);
                if (existing_identity == null)
                {
                    this.DB.LG_Customers.Attach(existing);
                    if (!String.IsNullOrEmpty(updated.Identity_Card))
                        existing.Identity_Card = updated.Identity_Card;
                    if (!String.IsNullOrEmpty(updated.Company_Name))
                        existing.Company_Name = updated.Contact_Name;
                    if (!String.IsNullOrEmpty(updated.Company_Name))
                        existing.Company_Name = updated.Company_Name;
                    if (!String.IsNullOrEmpty(updated.Address))
                        existing.Address = updated.Address;
                    if (!String.IsNullOrEmpty(updated.Post_Code))
                        existing.Post_Code = updated.Post_Code;
                    if (!String.IsNullOrEmpty(updated.City))
                        existing.City = updated.City;
                    if (!String.IsNullOrEmpty(updated.Country))
                        existing.Country = updated.Country;
                    if (!String.IsNullOrEmpty(updated.Phone))
                        existing.Phone = updated.Phone;
                    if (!String.IsNullOrEmpty(updated.Fax))
                        existing.Fax = updated.Fax;
                    this.DB.SubmitChanges();
                    return new Objects.ReturnBool() { Value = true };
                }
                else { return new Objects.ReturnBool() { Value = false, Reason = "Identity card is exist.." }; }
            }
            else { return new Objects.ReturnBool() { Value = false, Reason = "Can't foun the customer"}; }
        }

        public Objects.ReturnBool RemoveCustomer(int cust_id)
        {
            var existing = cust_repo.GetCustomerById(cust_id);
            if (existing != null)
            {
                this.DB.LG_Customers.Attach(existing);
                this.DB.LG_Customers.DeleteOnSubmit(existing);
                this.DB.SubmitChanges();
                return new Objects.ReturnBool() { Value = true };
            }
            else { return new Objects.ReturnBool() { Value = true }; }
        }
    }
}
